package controllers

import (
	"LoginTemplate/models"
	"time"
)

type DailyController struct {
	BaseController
}

type Daily struct {
	ID               int     `json:"id"`
	Month            int     `json:"month"`
	Marketing        string  `json:"marketing"`
	Client           string  `json:"client"`
	Phone            string  `json:"phone"`
	IdCard           string  `json:"id_card"`
	Product          string  `json:"product"`
	Specification    string  `json:"specification"`
	Quantity         int     `json:"quantity"`
	Price            float64 `json:"price"`
	Money            float64 `json:"money"`
	Receivable       float64 `json:"receivable"`
	OfficialReceipts float64 `json:"official_receipts"`
	Uncollected      float64 `json:"uncollected"`
	StaffName        string  `json:"staff_name"`
	Supervisor       string  `json:"supervisor"`
	ChiefInspector   string  `json:"chief_inspector"`
	City             string  `json:"city"`
	Account          string  `json:"account"`
	Remark           string  `json:"remark"`
	Order            string  `json:"order"`
	Date             string  `json:"date"`
}

// 添加分类
func (u *DailyController) InsertDaily() {
	var client Daily
	err := u.ShouldBind(&client)
	if err != nil {
		println(err.Error())
		u.JsonMsg(500, "", "请求异常，请稍候再试")
	}
	var daily Daily
	sql2 := "select * from daily where `order` = ? ORDER BY date DESC"
	models.DB.Debug().Raw(sql2, client.Order).Scan(&daily)
	if daily.Order == "" {
		sql1 := "insert into daily (month,marketing,client,phone,id_card,product,specification,quantity,price,money,receivable,official_receipts,uncollected,staff_name,supervisor,chief_inspector,city,account,remark,`order`,`date`) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
		tx := models.DB.Debug().Exec(sql1, client.Month, client.Marketing, client.Client, client.Phone, client.IdCard,
			client.Product, client.Specification, client.Quantity, client.Price, client.Money, client.Receivable, client.OfficialReceipts, client.Uncollected,
			client.StaffName, client.Supervisor, client.ChiefInspector, client.City, client.Account, client.Remark, client.Order, time.Now())
		if tx.Error != nil {
			u.JsonMsg(500, "", "失败")
		}
		u.JsonMsg(200, "", "成功")
	} else {
		sql1 := "insert into daily (month,marketing,client,phone,id_card,product,specification,quantity,price,money,receivable,official_receipts,uncollected,staff_name,supervisor,chief_inspector,city,account,remark,`order`,`date`) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
		tx := models.DB.Debug().Exec(sql1, client.Month, client.Marketing, client.Client, client.Phone, client.IdCard,
			client.Product, client.Specification, client.Quantity, client.Price, client.Money, 0, client.OfficialReceipts, daily.Uncollected-client.OfficialReceipts,
			client.StaffName, client.Supervisor, client.ChiefInspector, client.City, client.Account, client.Remark, client.Order, time.Now())
		if tx.Error != nil {
			u.JsonMsg(500, "", "失败")
		}
		u.JsonMsg(200, "", "成功")
	}
	//sql := "insert into client ( client_name,client_phone,client_classify,time,client_follow,promoter_id,community_id ) values (?,?,?,?,?,?,? )"
	//tx := models.DB.Debug().Exec(sql, client.ClientName, client.ClientPhone, client.ClientClassify, client.Time, client.ClientFollow, client.PromoterId, client.CommunityId)

}

//// 修改分类
//func (u *ClientController) UpdateDaily() {
//	var client Client
//	err := u.ShouldBind(&client)
//	if err != nil {
//		u.JsonMsg(500, "", "请求异常，请稍候再试")
//	}
//	sql := "update client set client_name = ?,client_phone = ?,client_classify = ?,time = ? ,client_follow = ? ,promoter_id = ? ,community_id = ? where id = ?"
//	tx := models.DB.Debug().Exec(sql, client.ClientName, client.ClientPhone, client.ClientClassify, client.Time, client.ClientFollow, client.PromoterId, client.CommunityId, client.ID)
//	if tx.Error != nil {
//		u.JsonMsg(500, "", "失败")
//	}
//	u.JsonMsg(200, "", "成功")
//}

type DailyData struct {
	Code        int     `json:"code"`
	Msg         string  `json:"msg"`
	Data        []Daily `json:"data"`
	Sum         int     `json:"sum"`         //总页数
	CurrentPage int     `json:"currentPage"` //当前页
	TotalNumber int     `json:"totalNumber"` //总条数
}

// 查询分类
func (u *DailyController) SelectDaily() {
	var pictureList PictureList
	err := u.ShouldBind(&pictureList)
	var sum int
	var totalNumber int
	if err != nil {
		println(err.Error())
		u.JsonMsg(500, "", "请求异常，请稍候再试")
	}
	var client []Daily
	sql := "select * from daily limit ? , ?"
	tx := models.DB.Debug().Raw(sql, (pictureList.Page-1)*pictureList.Size, pictureList.Size).Scan(&client)
	sqlsum := "SELECT CEIL(COUNT(*) / ? ) FROM daily"
	models.DB.Debug().Raw(sqlsum, pictureList.Size).Scan(&sum)
	sqlNumBer := "select count(*) from daily"
	models.DB.Debug().Raw(sqlNumBer).Scan(&totalNumber)
	if tx.Error != nil {
		u.JsonMsg(500, "", "请求异常，请稍候再试")
	}
	var picturedata DailyData
	picturedata.Code = 200
	picturedata.Data = client
	picturedata.Msg = "成功"
	picturedata.Sum = sum
	picturedata.CurrentPage = pictureList.Page
	picturedata.TotalNumber = totalNumber
	//return picturedata
	u.JsonMsg(200, map[string]interface{}{
		"list":  picturedata,
		"count": "",
	}, "success")
}
